﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework.SyntaxHelpers;

namespace NSsh.Common.Tests
{
    [TestFixture]
    public class ThrowsConstraintFixture
    {
        [Test]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "Exception expected.")]
        public void ShouldThrowArgumentExceptionForInvalidType()
        {
            bool exceptionThrown = false;

            try
            {
                new Throws(typeof(object));
            }
            catch (ArgumentException)
            {
                exceptionThrown = true;
            }

            Assert.That(exceptionThrown, Is.True);
        }

        [Test]
        public void ShouldThrowArgumentExceptionForNonDelegateParameter()
        {
            bool exceptionThrown = false;

            try
            {
                Throws t = new Throws(typeof(InvalidCastException));

                bool matches = t.Matches(new object());
            }
            catch (ArgumentException)
            {
                exceptionThrown = true;
            }

            Assert.That(exceptionThrown, Is.True);
        }

        [Test]
        public void ShouldPassForCorrectExceptionType()
        {
            Throws t = new Throws(typeof(InvalidCastException));

            bool matches = t.Matches(new MethodInvoker(delegate
            {
                throw new InvalidCastException();
            }));

            Assert.That(matches, Is.True);
        }

        [Test]
        public void ShouldFailForIncorrectExceptionType()
        {
            Throws t = new Throws(typeof(InvalidCastException));

            bool matches = t.Matches(new MethodInvoker(delegate
            {
                throw new InvalidOperationException();
            }));

            Assert.That(matches, Is.False);
        }

        [Test]
        public void ShouldFailForNoExceptionThrown()
        {
            Throws t = new Throws(typeof(InvalidCastException));

            bool matches = t.Matches(new MethodInvoker(delegate
            {
                Console.WriteLine("Hi");
            }));

            Assert.That(matches, Is.False);
        }
    }
}
